//
//  JCLTextView.swift
//  WeiBo
//
//  Created by DDC on 2017/9/27.
//  Copyright © 2017年 DDC. All rights reserved.
//

import UIKit

class JCLTextView: UITextView {

    // 懒加载占位标签
    fileprivate lazy var placeholderLabel = UILabel()
    
    /// 获取微博表情文本的字符串
    var emoticonString:String {
        
        guard let attrText = attributedText else {
            return ""
        }
        
        var result = String()
        attrText.enumerateAttributes(in: NSRange(location:0,length:attrText.length), options: []) { (dict, range, _) in
            
            
            
            if let attachment = dict["NSAttachment"] as? JCLEmoticonAttachment {
                
                
                result += attachment.chs ?? ""
            } else {
                let subStr = (attrText.string as NSString).substring(with: range)
                result +=  subStr
            }
            
        }
        return result
    }
    
    

    override func awakeFromNib() {
        
        setupUI()
    }
    // 移除通知
    deinit {
        NotificationCenter.default.removeObserver(self)
    }
}

// MARK: - 设置textView
fileprivate extension JCLTextView {
    func setupUI () {
        
        // 设置占位标签的属性
        placeholderLabel.text = "分享新鲜事..."
        placeholderLabel.font = self.font
        placeholderLabel.textColor = UIColor.lightGray
        placeholderLabel.sizeToFit()
        placeholderLabel.frame.origin = CGPoint(x: 5, y: 8)
        self.addSubview(placeholderLabel)
        
        // 发送通知监听占位标签的显示与隐藏
        NotificationCenter.default.addObserver(self, selector: #selector(textViewDidChange), name: .UITextViewTextDidChange, object: self)
    }
}
// MARK: - 监听方法
fileprivate extension JCLTextView {
    
    /// 通知监听方法
    @objc func textViewDidChange () {
        
        placeholderLabel.isHidden = self.hasText
    }
}

// MARK: - 点击插入表情键盘的方法
extension JCLTextView {
    func insertEmoticon (emo:JCLEmoticon?) {
        // 删除按钮实现方法
        guard let emo = emo else {
           deleteBackward()
            return
        }
        // emoji 表情插入
        if let emoji = emo.emojiStr, let textRange = selectedTextRange {
            replace(textRange, withText: emoji)
            
            return
        }
        // 默认表情--图片表情
        // 所有插入排版几乎都有一个共同的属性,插入的字符的显示，会更随前一个字符的属性,但自身 是没有属性的---故要统一设置属性
        let imageText = NSMutableAttributedString(attributedString: emo.imageText(textFont: font!))
        imageText.addAttributes([NSFontAttributeName:font!], range: NSRange(location: 0, length: 1))
        let emoAttrM = NSMutableAttributedString(attributedString: attributedText)
        // 记录range
        let range = selectedRange
        emoAttrM.replaceCharacters(in: selectedRange, with: imageText)
        attributedText = emoAttrM
        // 恢复光标位置
        selectedRange = NSRange(location: range.location + 1, length: 0)
        // 响应代理方法
        delegate?.textViewDidChange?(self)
        // 响应textView监听方法
        textViewDidChange()
        
    }
}
